package com.xbts.web.controller.manager;

import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.core.page.TableDataInfo;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnContractManager;
import com.xbts.system.domain.CnOrderItem;
import com.xbts.system.domain.dto.CnContractManagerDTO;
import com.xbts.system.domain.vo.CnContractManagerVO;
import com.xbts.system.service.ICnContractManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合同管理Controller
 * @author xbts
 * @date 2023-08-29
 */
@Api("合同管理")
@RestController
@RequestMapping("/system/contract")
public class CnContractManagerController extends BaseController {

    @Autowired
    private ICnContractManagerService cnContractManagerService;

    /**
     * 查询合同管理列表
     */
    @ApiOperation("查询合同管理列表")
    @PreAuthorize("@ss.hasPermi('system:manager:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CnContractManagerVO cnContractManager) {
        startPage(cnContractManager);
        List<CnContractManager> list = cnContractManagerService.selectCnContractManagerList(cnContractManager);
        return getDataTable(list);
    }

    /**
     * 查询未开订单的合同列表
     */
    @ApiOperation("查询未开订单的合同列表")
    @PreAuthorize("@ss.hasPermi('system:manager:list')")
    @GetMapping("/orderList/{shoppingCarEnums}")
    public TableDataInfo orderList(@PathVariable String shoppingCarEnums) {
        startPage();
        List<CnOrderItem> list = cnContractManagerService.orderList(shoppingCarEnums);
        return getDataTable(list);
    }

    /**
     * 合同管理下载
     */
    @ApiOperation("合同管理下载")
    @PreAuthorize("@ss.hasPermi('system:manager:export')")
    @Log(title = "合同管理下载", businessType = BusinessType.EXPORT)
    @ApiImplicitParam(name = "ids", value = "子订单ids", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PostMapping("/download")
    public void download(HttpServletResponse response, @RequestBody List<String> ids) {
        cnContractManagerService.download(response, ids);
    }

    /**
     * 获取合同管理详细信息
     */
    @ApiOperation("获取合同管理详细信息")
    @ApiImplicitParam(name = "id", value = "合同管理ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:manager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(cnContractManagerService.selectCnContractManagerById(id));
    }

    /**
     * 新增合同
     */
    @ApiOperation("新增合同")
    @PreAuthorize("@ss.hasPermi('system:manager:add')")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CnContractManagerDTO cnContractManager) {
        return toAjax(cnContractManagerService.insertCnContractManager(cnContractManager));
    }

    /**
     * 修改合同管理
     */
    @ApiOperation("修改合同管理")
    @PreAuthorize("@ss.hasPermi('system:manager:edit')")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CnContractManager cnContractManager) {
        return toAjax(cnContractManagerService.updateCnContractManager(cnContractManager));
    }

    /**
     * 删除合同管理
     */
    @ApiOperation("删除合同管理")
    @ApiImplicitParam(name = "ids", value = "合同管理ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:manager:remove')")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> ids) {
        return toAjax(cnContractManagerService.deleteCnContractManagerByIds(ids));
    }


    /**
     * 作废合同管理
     */
    @ApiOperation("作废合同管理")
    @PreAuthorize("@ss.hasPermi('system:manager:remove')")
    @Log(title = "作废合同管理", businessType = BusinessType.DELETE)
    @GetMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable String id) {
        return toAjax(cnContractManagerService.cancel(id));
    }

    /**
     * 通过
     */
    @GetMapping("/pass/{orderItemId}")
    public AjaxResult pass(@PathVariable String orderItemId){
        return success(cnContractManagerService.pass(orderItemId));
    }

    /**
     * 拒绝
     */
    @GetMapping("/refuse/{orderItemId}")
    public AjaxResult refuse(@PathVariable String orderItemId){
        return success(cnContractManagerService.refuse(orderItemId));
    }
}
